*Replication for: TEELE (2023) The Political Geography of the Gender Gap.
*Generates Figure 3

********************************************************
*	1. Construct Bm, estimates of men's behavior in 1921
*	2. Construct Bw, estimates of women's behavior in 1921
*		>save data as simulation.dta
*  	3. Generate figure 3a 
*	4. Generate figure 3b
********************************************************




*************************************************
*1. Construct Bm: Counterfactual estimates of men's behavior using most and least left year in each parish. 
*************************************************
use "Sweden 1911-1944_panel.dta", clear
keep if year<=1924

	*Left Lag and Lag2
		sum left if year<1921
		sort ID year
		
		by ID: gen L1=left[_n-1]
		by ID: gen L2=left[_n-2]
		
		la var L1 "Left previous election" 
		la var L2 "Left penultimate election" 
	
	*Left Difference and Difference 2
		gen LD=left-L1 if L1!=.
		gen LD2=L1-L2 if L1!=. & L2!=.
		
		la var LD "Left change L-L1 " 
		la var LD2 "Left change L1-L2"
		
	
	*Most Left Year 	
		by ID: egen a=max(left) if year<1921 & year!=. /*pull out the highest value of party vote share in earlier elections*/ 
		by ID: egen maxleft=max(a) /*then fill it in for the whole column for that parish */ 
		replace maxleft=1 if maxleft>1 & maxleft!=.
		
	*Sanity Check: Maximum leftism possible for men is left percent/# men who voted. 
		
		gen mostleft=left/VTmen & year==1921
		di "sanity check" 
		replace maxleft=mostleft if maxleft>mostleft & maxleft!=. & year==1921
		
		la var maxleft "Men most left year"
		drop a mostleft
		
	*Least Left Year 	
		by ID: egen a=min(left) if year<1921 & year!=. /*pull out the highest value of party vote share in earlier elections*/ 
		by ID: egen minleft=max(a) /*then fill it in for the whole column */ 
		replace minleft=0 if minleft<0 & maxleft!=.
		
		la var minleft "Men least left year"
		drop a 
		
		
*Construct estimate of number of new voters and old voters 		
		
		*First construct a measure of "old" and "new" male voters using the average male votes cast in prior elections. 
		egen avgnvotes=mean(VTmen) if inlist(year, 1914, 1917, 1920), by(ID)
		egen VTm_av=max(avgnvotes), by(ID) /*fills in the average male votes cast for later elections*/ 
		scatter VTmen VTm_av if year==1921
		gen VT_new=VTmen-VTm_av if year==1921
		*get rid of negative values 
		replace VT_new =0 if VT_new <0 & VT_new !=. & year==1921
		replace VT_new =. if VTmen==.  & year==1921
		
		gen VT_old =VTmen-VT_new /*n votes cast old men*/ 

		assert VT_new+VT_old==VTmen if year==1921
		
		drop avg VTm_av
		la var VT_old "Estimate of Old Male Voters 1921" 
		la var VT_new  "Estimate of New Male Voters 1921" 
		
		
		
		
*****************************************
*2. Bw: Simulation of Women's Leftism 
*****************************************
		
	*Duncan Davis equation BwL = (left - BmL*(1-wshare))/wshare 
	*My estimation strategy picks different values of BmL (men's leftism) and simulates women's choice using the above equation.

		foreach Bm in 0 1 maxleft L1 L2 {

		gen Bw_m`Bm'= (left - `Bm'*(1-womensharecast ))  / womensharecast if year==1921
	
		*substitute with realistic values 
		replace Bw_m`Bm'=1 if Bw_m`Bm'>=1 & Bw_m`Bm'!=.
		replace Bw_m`Bm'=0 if Bw_m`Bm'<=0 & Bw_m`Bm'!=.
		
		}

		la var Bw_m1 "Women | All men left" 
		la var Bw_m0 "Women | No men left"
		la var Bw_mmaxleft "Women | Men Most Left Year"
		la var Bw_mL1 "Women | Men =1920"
		la var Bw_mL2 "Women | Men =1917"
		
		
		save simulation.dta, replace 
		
		
***************************************************************************
*3. Figure 3a: Distribution of men's behavior in previous elections, and with counterfactual of the least left and most left year in each municipality. By urban versus rural parishes.  
***************************************************************************

forvalues n = 4(4)12 {
	local xlab `xlab' `n' "`=string(round(exp(`n'),10),"%9.0fc")'"
	}
	
	*syntax for the rug plot below the figure 3a
	cap gen pos=0  
	cap gen pipe = "|"

	# delimit ; 
			
		twoway 		
			(lowess maxleft lElect, bwidth(.3) lcolor(red) lwidth(thick) lpattern(solid) )
			(lowess L2 lElect , bwidth(.3) lwidth(thick) lpattern(longdash) lcolor(gs6))
			(lowess L1 lElect , bwidth(.3) lwidth(thick) lpattern(dot) lcolor(gs6))
			(lowess left lElect , bwidth(.3)lwidth(thick) lpattern(solid) lcolor(black))
			(lowess minleft lElect , bwidth(.3) lcolor(midblue) lwidth(thick) lpattern(solid) ) 
			(scatter pos  lElect,   ms(none) mcolor(gs16) mla(pipe) mlabpos(0))
				
			if year==1921 & red21==0

			, 	
			by(City, note("") )  
			ytitle("Votes Cast for the Left (%)", size(large)) 
			xtitle("Electorate Size (ln)", size(large))  
			legend(label(1 "Men, Most left year") label(2 "Men 1917") label(3 "Men 1920") label(4 "1921 election (Men & Women) ") label(5 "Men, Least left year") 			order(1 2 3 4 5) col(3) size(small) ring(1) pos(3)) 
			xlab(`xlab')
			ylab(0(.2)1) 
			scheme(tufte)
				; 
	# delimit cr
		 		
			graph export "./images/Fig3a.eps", replace 	 
		
	
		

******************************************
*Fig 3b: Simulated vote choice for women (Bw) given counterfactual estimates of Bm in rural and urban locales. 
******************************************	
forvalues n = 4(4)12 {
	local xlab `xlab' `n' "`=string(round(exp(`n'),10),"%9.0fc")'"
	}
				
	# delimit ; 
			
		twoway 		
			(lowess left lElect , bwidth(.3)lwidth(thick) lpattern(solid) lcolor(gs6))
			(lowess Bw_m0  lElect  , bwidth(.3)lcolor(gs12) lwidth(thick) lpattern(solid) )
			(lowess Bw_mL1 lElect  , bwidth(.3)lcolor(red) lwidth(thick) lpattern(solid) )
			(lowess Bw_mL2 lElect  , bwidth(.3)lcolor(maroon) lwidth(thick) lpattern(solid) )
			(lowess Bw_mmax lElect  , bwidth(.3)lcolor(pink) lwidth(thick) lpattern(solid) )
			(lowess Bw_m1 lElect , bwidth(.3)lcolor(gs12) lwidth(thick) lpattern(solid) ) 
			(scatter pos lElect ,   ms(none) mcolor(gs16) mla(pipe) mlabpos(0))
							
				if year==1921 & red21==0

				, 
			by(City, note("") ) 
			ytitle("Predicted Votes for the Left (%)", size(large)) 			
			xtitle(Electorate Size, size(large))  legend(off)
			xlab(`xlab')
			ylab( 0(.2)1 )  
			legend(label(1 "1921 Actual Returns") label(3 "Women | Men=1920") label (4 "Women | Men=1917") label(5 "Women | Men=Most Left Year") 
			order(1 3 4 5) col(2) size(small)) scheme(tufte) 
				; 
		# delimit cr
		
	 	graph export "./images/Fig3b.eps", replace 
	
		
		

	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
